package pl.uz.zgora.plan;

import java.io.IOException;
import java.util.ArrayList;

import org.ksoap2.SoapFault;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class SelectStudentDataActivity extends Activity {
  
	private SOAPManager mSoapManager = null;
	final private ArrayList<Kierunek> specializationList = new ArrayList<Kierunek>();
	final private ArrayList<Grupa> groupsList = new ArrayList<Grupa>();
	
	private ProgressDialog mProgressDialog = null;
	private Grupa selectedGroup = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_student_data_view);
        
        PrefrencesManager pm =  PrefrencesManager.getInstance(getBaseContext());
		pm.addBooleanToPreferences("hasData", false);
        
        if(HelperMethodsManager.isOnline(this)) {

        	mSoapManager = new SOAPManager(this);
        	setupUI();

        	mProgressDialog = HelperMethodsManager.getProgressDialog(this);
        	mProgressDialog.show();

        	Thread handleActivity = new Thread(new Runnable() {

        		public void run() {
        			String[] specializationNames = null;
        			try {
        				specializationNames = mSoapManager.downloadSpecializationList(specializationList);
        				manageSpecializationUI(specializationNames);
        			} catch (SoapFault e) {
        				mProgressDialog.dismiss();
        				HelperMethodsManager.manageException("SelectStudentDataActivity::onCreate()", "SoapFault", e.getLocalizedMessage(), SelectStudentDataActivity.this);
        			} catch (IOException e) {
        				mProgressDialog.dismiss();
        				HelperMethodsManager.manageException("SelectStudentDataActivity::onCreate()", "IOException", e.getLocalizedMessage(), SelectStudentDataActivity.this);
        			} catch (XmlPullParserException e) {
        				mProgressDialog.dismiss();
        				HelperMethodsManager.manageException("SelectStudentDataActivity::onCreate()", "XmlPullParserException", e.getLocalizedMessage(), SelectStudentDataActivity.this);
        			} catch(Exception e) {
        				mProgressDialog.dismiss();
        				HelperMethodsManager.manageException("SelectStudentDataActivity::onCreate()", "Exception", e.getLocalizedMessage(), SelectStudentDataActivity.this);
        			}

        		}
        	});

        	handleActivity.start();
        } else {
        	Dialog errorDialog = HelperMethodsManager.getConnectionErrorDialog(this, this);
        	errorDialog.show();
        }
    }
    
    /** 
	 * Obsługa (UI) listy rozwijanej dla grup na wybranych kierunkach
	 */
	private void manageGroupsUI(final String[] groupsNames)
	{
		final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, groupsNames);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		final LinearLayout mainLayout = (LinearLayout) findViewById(R.id.select_student_data_layout);
		final Spinner groupsSpinner = (Spinner) findViewById(R.id.spinnerGroups);
		final LinearLayout groupsSpinnerLayout = (LinearLayout) findViewById(R.id.spinnerGroupsLayout);
		final TextView tv_group_label = (TextView) findViewById(R.id.tv_group_label);
		final Button nextButton = (Button) findViewById(R.id.buttonNext);
		groupsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					int pos, long id) {
				
				nextButton.setVisibility(Button.VISIBLE);
				selectedGroup = groupsList.get(pos);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		
		mainLayout.post(new Runnable() {
			
			public void run() {
				
				mProgressDialog.dismiss();
				tv_group_label.setVisibility(View.VISIBLE);
				groupsSpinner.setVisibility(View.VISIBLE);
				groupsSpinnerLayout.setVisibility(View.VISIBLE);
				groupsSpinner.setAdapter(adapter);
			}
		});
	}
    
    /** 
	 * Obsługa (UI) listy rozwijanej dla kierunków studiów
	 */
	private void manageSpecializationUI(final String[] specializationNames)
	{
		final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, specializationNames);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		final LinearLayout mainLayout = (LinearLayout) findViewById(R.id.select_student_data_layout);
		final Spinner specializationSpinner = (Spinner) findViewById(R.id.spinnerSpecializations);
		final LinearLayout specializationSpinnerLayout = (LinearLayout) findViewById(R.id.spinnerSpecializationsLayout);
		final Spinner groupsSpinner = (Spinner) findViewById(R.id.spinnerGroups);
		final LinearLayout groupsSpinnerLayout = (LinearLayout) findViewById(R.id.spinnerGroupsLayout);
		final Button nextButton = (Button) findViewById(R.id.buttonNext);
		final TextView tv_specialization_label = (TextView) findViewById(R.id.tv_specialization_label);
		final TextView tv_group_label = (TextView) findViewById(R.id.tv_group_label);
		specializationSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View view,
					final int pos, long id) {
				
				Thread handleActivity = new Thread(new Runnable() {
		    		
		    		public void run() {
		    			String[] groupsNames = null;
						try {
							groupsNames = mSoapManager.downloadGroupsList(specializationList.get(pos).getIdKierunku(), groupsList);
							
							mainLayout.post(new Runnable() {
								
								public void run() {
									groupsSpinner.setVisibility(View.INVISIBLE);
									groupsSpinnerLayout.setVisibility(View.INVISIBLE);
									nextButton.setVisibility(View.INVISIBLE);
									tv_group_label.setVisibility(View.INVISIBLE);
									tv_specialization_label.setVisibility(View.VISIBLE);
								}
							});
							
							manageGroupsUI(groupsNames);
						} catch (SoapFault e) {
							mProgressDialog.dismiss();
							HelperMethodsManager.manageException("SelectStudentDataActivity::manageSpecializationUI()", "SoapFault", e.getLocalizedMessage(), SelectStudentDataActivity.this);
						} catch (IOException e) {
							mProgressDialog.dismiss();
							HelperMethodsManager.manageException("SelectStudentDataActivity::manageSpecializationUI()", "IOException", e.getLocalizedMessage(), SelectStudentDataActivity.this);
						} catch (XmlPullParserException e) {
							mProgressDialog.dismiss();
							HelperMethodsManager.manageException("SelectStudentDataActivity::manageSpecializationUI()", "XmlPullParserException", e.getLocalizedMessage(), SelectStudentDataActivity.this);
						} catch(Exception e) {
							mProgressDialog.dismiss();
							HelperMethodsManager.manageException("SelectStudentDataActivity::manageSpecializationUI()", "Exception", e.getLocalizedMessage(), SelectStudentDataActivity.this);
						}
						
		    		}
		        });
		        
		        handleActivity.start();
				
				mProgressDialog.show();
				
				
			}

			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
		
		mainLayout.post(new Runnable() {
			
			public void run() {
				mProgressDialog.dismiss();
				specializationSpinner.setVisibility(View.VISIBLE);
				specializationSpinnerLayout.setVisibility(View.VISIBLE);
				specializationSpinner.setAdapter(adapter);
			}
		});
		
	}
    
    private void setupUI() {
    	Button btnNext = (Button) findViewById(R.id.buttonNext);
    	btnNext.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {

				final Intent intent = new Intent(v.getContext(), SelectSubgroupActivity.class);
				intent.putExtra("idGrupy", selectedGroup.getIdGrupy());
				intent.putExtra("nazwaGrupy", selectedGroup.getNazwa());
				
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				
				try {
					// Uruchomienie menu głównego
					startActivity(intent);
					setResult(0);
	                finish();
				} catch (android.content.ActivityNotFoundException e) {
					HelperMethodsManager.manageException("setupUI()", e.getLocalizedMessage(), SelectStudentDataActivity.this);
				}
			}
		});
    }
}